home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / c / amivogl-1.03.lzh / vogl / examples / tetra.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-16  |  3.1 KB  |  187 lines

  1. /*
  2.  * Demonstrate a rotating translating tetrahedron.
  3.  */
  4.  
  5. #include <stdio.h>
  6.  
  7. #ifdef SGI
  8. #include "gl.h"
  9. #include "device.h"
  10. #include "hershey.h"
  11. #else
  12. #include "vogl.h"
  13. #include "vodevice.h"
  14. #endif
  15.  
  16. #ifndef TC
  17. #include <math.h>
  18. #else
  19. extern double sin(), cos();
  20. #endif
  21.  
  22. #define    TETRAHEDRON    1L
  23. #define    NSIDES    3
  24. #define    NFACES    4
  25. #define    NPNTS    4
  26.  
  27. Coord    points[NPNTS][3] = {
  28.     {-0.5, 0.866, -0.667},
  29.     {-0.5, -0.866, -0.667},
  30.     { 1.0, 0.0, -0.667},
  31.     { 0.0, 0.0, 1.334}
  32. };
  33.  
  34. int    faces[NFACES][NSIDES] = {
  35.     {2, 1, 0},
  36.     {0, 1, 3},
  37.     {1, 2, 3},
  38.     {2, 0, 3}
  39. };
  40.  
  41. int    colface[NFACES] = {
  42.         GREEN,
  43.         YELLOW,
  44.         CYAN,
  45.         MAGENTA
  46. };
  47.  
  48. /* ---------------------------------------------------------------------
  49.  * Prototypes:
  50.  */
  51. int main( int, char **);                               /* tetra.c         */
  52. int maketetra(void);                                   /* tetra.c         */
  53.  
  54. /* ---------------------------------------------------------------------
  55.  * Source:
  56.  */
  57.  
  58. int main(
  59.   int argc,
  60.   char **argv)
  61. {
  62.     char    dev[20];
  63.     int    i, but;
  64.     int    rotval = 0, drotval = 2;
  65.     float    R = 1.6, tx = 0.0, tz = R, zeye = 5.0;
  66.     int    do_backface = 0;
  67.     int    do_fill = 0;
  68.     short    val;
  69.  
  70.     for (i = 1; i < argc; i++) {
  71.         if (!strcmp(argv[i], "-b"))
  72.             do_backface = 1;
  73.         if (!strcmp(argv[i], "-f"))
  74.             do_fill = 1;
  75.     }
  76.  
  77.     prefsize(400L, 400L);
  78.  
  79.     winopen("tetra");          /* set up device */
  80.  
  81.     qdevice(ESCKEY);
  82.     qdevice(QKEY);
  83.     unqdevice(INPUTCHANGE);
  84.  
  85.     doublebuffer();
  86.     gconfig();
  87.  
  88.  
  89.     polymode(PYM_LINE);
  90.     if (do_fill)
  91.         polymode(PYM_FILL);
  92.  
  93.     if (do_backface)
  94.         backface(1);
  95.  
  96.     /*
  97.      * set up a perspective projection with a field of view of
  98.      * 40.0 degrees, aspect ratio of 1.0, near clipping plane 0.1,
  99.      * and the far clipping plane at 1000.0.
  100.      */
  101.     perspective(400, 1.0, 0.001, 15.0);
  102.     lookat(0.0, 0.0, zeye, 0.0, 0.0, 0.0, 0);
  103.  
  104.  
  105.     /*
  106.      * Make a tetrahedron object
  107.      */
  108.  
  109.     maketetra();
  110.  
  111.     do {
  112.         for (rotval = 0; rotval < 3600; rotval += drotval) {
  113.             color(BLACK);
  114.             clear();
  115.  
  116.             /*
  117.              * Rotate the whole scene...(this acumulates - hence
  118.              * drotval)
  119.              */
  120.             rotate(drotval, 'x');
  121.             rotate(drotval, 'z');
  122.  
  123.             color(RED);
  124.             pushmatrix();
  125.                 rotate(900, 'x');
  126.                 circ(0.0, 0.0, R);
  127.             popmatrix();
  128.  
  129.             color(BLUE);
  130.             move(0.0, 0.0, 0.0);
  131.             draw(tx, 0.0, tz);
  132.             
  133.             /*
  134.              * Remember! The order of the transformations is
  135.              * the reverse of what is specified here in between
  136.              * the pushmatrix and the popmatrix. These ones don't
  137.              * accumulate because of the push and pop.
  138.              */
  139.             pushmatrix();
  140.                 translate(tx, 0.0, tz);
  141.                 rotate(rotval, 'x');
  142.                 rotate(rotval, 'y');
  143.                 rotate(rotval, 'z');
  144.                 scale(0.4, 0.4, 0.4);
  145.                 callobj(TETRAHEDRON);
  146.             popmatrix();
  147.  
  148.             tz = R * cos((double)(rotval * 3.1415926535 / 180));
  149.             tx = R * sin((double)(rotval * 3.1415926535 / 180));
  150.  
  151.             swapbuffers();
  152.  
  153.             if (qtest()) {
  154. /*
  155.                 but = (int)qread(&val);
  156.                 fprintf(stderr, "but = %c (%d)\n", but, but);
  157. */
  158.                 gexit();
  159.                 exit(0);
  160.             }
  161.         }
  162.  
  163.     } while (1);
  164. }
  165.  
  166. /*
  167.  * maketetra
  168.  *
  169.  *    draw a tetrahedron
  170.  */
  171. int maketetra(void)
  172. {
  173.     int    i, j;
  174.  
  175.     makeobj(TETRAHEDRON);
  176.  
  177.     for (i = 0; i < NFACES; i++) {
  178.         color(colface[i]);
  179.         bgnpolygon();
  180.             for (j = 0; j < NSIDES; j++) 
  181.                 v3f(points[faces[i][j]]);
  182.         endpolygon();
  183.     }
  184.  
  185.     closeobj();
  186. }
  187.